import java.awt.geom.Point2D;

/**
 * @author Yaqi Zhang
 */
public class Bar extends Spring {

    private static final double BAR_STRINGYNESS = 5;
    /**
     * @param start start mass
     * @param end end mass
     *        Pass start mass and end mass to super constructor.
     *        Set the distance between two mass to be length.
     *        Set the springy-ness coefficient to be 0.
     */
    public Bar(Mass start, Mass end) {
        super(start, end, Force.distanceBetween(start.getCenter(),
                end.getCenter()), BAR_STRINGYNESS);
        // TODO Auto-generated constructor stub
    }

    /**
     * @param canvas canvas to draw on
     * @param dt time interval to refresh the screen
     *        update the movement of a bar
     */
    @Override
    public void update(Simulation canvas, double dt) {
        super.update(canvas, dt);
        setEndCenter();
    }
    /**
     * set the end of the bar to make the bar have fixed length.
     */
    private void setEndCenter() {
        double startX = super.getStart().getCenter().getX();
        double startY = super.getStart().getCenter().getY();
        double endX = super.getEnd().getCenter().getX();
        double endY = super.getEnd().getCenter().getY();
        double dx = endX - startX;
        double dy = endY - startY;
        double ratio = super.getLength()
                / Point2D.distance(startX, startY, endX, endY);
        super.getEnd().setCenter(startX + dx * ratio, startY + dy * ratio);
    }
}
